package dashboard;

import java.io.IOException;
import java.io.ObjectStreamException;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.LinkedHashMap;

import javax.servlet.http.*;

@SuppressWarnings("serial")
public class LeaderboardServlet extends HttpServlet {

	private User currentUser;
	private HttpSession session;
	private HttpServletRequest req;
	private HttpServletResponse resp;
	private ActivityController ac;

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		this.req= req;
		this.resp = resp;
		session = req.getSession();	
		User user =  (User) session.getAttribute("user");
		currentUser = UserRegistry.getInstance().getUserById(user.getId());
		String type = (String) req.getParameter("type");
		if(type!=null){

			String course = (String) req.getParameter("course");


			if(type.equals("allBoard"))getAllBoard();
			else if(type.equals("courseBoard")) getCourseBoard(course);
			else if(type.equals("milestonesBoard")) getMilestonesBoard();
			else if(type.equals("allPages")) getAllPages ();
			else if(type.equals("coursePages")) getCoursePages(course);
			else if(type.equals("relativeAllPages")) getRelativeAllPages();
			else if(type.equals("courseAllPages")) getRelativeCoursePages(course);
			else{
				String error = "You can't select this";
				session.setAttribute("error",error);
				resp.sendRedirect("leaderboard.jsp");
			}

		}
	}




	public void getAllBoard() throws IOException, ObjectStreamException{

		Map<String, Double> topTen = new LinkedHashMap<String,Double>();
		Map<String, Double> userDurations = Statistics.getTotalDurationUsers();
	
		Map<String, Double> sortedMap = LeaderboardController.sortMap(Statistics.getTotalDurationUsers());
		int ownPosition = LeaderboardController.getPos(sortedMap,currentUser);
		double ownValue = Statistics.getTotalDuration(currentUser, currentUser);

		LinkedHashMap<Integer, Double> favouritesBoard = new LinkedHashMap<Integer, Double>();
		List<String> favourites = currentUser.getFavourites();

		if (favourites!=null){
			for(String favourite: favourites){
				User user = UserRegistry.getInstance().getUser(favourite);
				favouritesBoard.put(LeaderboardController.getPos(Statistics.getTotalDurationUsers(), user), Statistics.getTotalDuration(user,user));

			}
		}
		session.setAttribute("size",userDurations.size());
		session.setAttribute("ownPosition", ownPosition);
		session.setAttribute("ownValue", ownValue);
		session.setAttribute("favouritesBoard",favouritesBoard);
		session.setAttribute("favourites",favourites);
		
		topTen = LeaderboardController.getTopTen(Statistics.getTotalDurationUsers());
		
		session.setAttribute("allBoard",topTen);
		resp.sendRedirect("leaderboard.jsp?type=allBoard");

	}
	public void getCourseBoard(String courseName) throws IOException{
		if(courseName==null){
			session.setAttribute("error","No valid coursename");
			resp.sendRedirect("leaderboard.jsp");
		}
		Map<String, Double> userDurations = LeaderboardController.getTotalDurationOfCourseForAllUsers(courseName);
		Map<String, Double> sortedMap = LeaderboardController.sortMap(userDurations);

		int ownPosition = LeaderboardController.getPos(sortedMap,currentUser);
		double ownValue = Statistics.getCourseDurationUser(currentUser, CourseRegistry.getInstance().getCourseId(courseName));

		LinkedHashMap<Integer, Double> favouritesBoard = new LinkedHashMap<Integer, Double>();
		List<String> favourites = currentUser.getFavourites();

		if (favourites!=null){
			for(String favourite: favourites){
					User user = UserRegistry.getInstance().getUser(favourite);
					favouritesBoard.put(LeaderboardController.getPos(LeaderboardController.getTotalDurationOfCourseForAllUsers(courseName), user), Statistics.getCourseDurationUser(user, CourseRegistry.getInstance().getCourseId(courseName)));

			}
		}
		session.setAttribute("size",LeaderboardController.getTotalDurationOfCourseForAllUsers(courseName).size());
		session.setAttribute("ownPosition", ownPosition);
		session.setAttribute("ownValue", ownValue);
		session.setAttribute("favouritesBoard",favouritesBoard);
		session.setAttribute("favourites",favourites);
		
		Map<String, Double> topTen = LeaderboardController.getTopTen(LeaderboardController.getTotalDurationOfCourseForAllUsers(courseName));
		session.setAttribute("allBoard",topTen);
		
		session.setAttribute("course",courseName);
		resp.sendRedirect("leaderboard.jsp?type=allBoard&course="+courseName);
	}

	public void getMilestonesBoard()throws IOException{

		Map<String, Double> userMilestones = LeaderboardController.getTotalSucceededMilestonesPerUser();
		Map<String, Double> sortedMap = LeaderboardController.sortMap(LeaderboardController.getTotalSucceededMilestonesPerUser());
		int ownPosition = LeaderboardController.getPos(sortedMap,currentUser);
		double ownValue = Statistics.amountOfAchievedMilestones(currentUser);

		LinkedHashMap<Integer, Double> favouritesBoard = new LinkedHashMap<Integer, Double>();
		List<String> favourites = currentUser.getFavourites();

		if (favourites!=null){
			for(String favourite: favourites){
				User user = UserRegistry.getInstance().getUser(favourite);
				favouritesBoard.put(LeaderboardController.getPos(LeaderboardController.getTotalSucceededMilestonesPerUser(), user), Statistics.amountOfAchievedMilestones(user));
			}
		}
		session.setAttribute("size",LeaderboardController.getTotalSucceededMilestonesPerUser().size());
		session.setAttribute("ownPosition", ownPosition);
		session.setAttribute("ownValue", ownValue);
		session.setAttribute("favouritesBoard",favouritesBoard);
		session.setAttribute("favourites",favourites);
		Map<String, Double> topTen = LeaderboardController.getTopTen(userMilestones);
		session.setAttribute("allBoard",topTen);
		resp.sendRedirect("leaderboard.jsp?type=milestonesBoard");

	}

	public void getAllPages() throws IOException{
		Map<String, Double> userPages = Statistics.getTotalPagesByUser();

		int ownPosition = LeaderboardController.getPos(Statistics.getTotalPagesByUser(),currentUser);
		double ownValue = Statistics.getPagesAllCoursesPerUser(currentUser);

		LinkedHashMap<Integer, Double> favouritesBoard = new LinkedHashMap<Integer, Double>();
		List<String> favourites = currentUser.getFavourites();

		if (favourites!=null){
			for(String favourite: favourites){
				User user = UserRegistry.getInstance().getUser(favourite);
				favouritesBoard.put(LeaderboardController.getPos(Statistics.getTotalPagesByUser(), user), Statistics.getPagesAllCoursesPerUser(user));

			}
		}
		session.setAttribute("size",Statistics.getTotalPagesByUser().size());
		session.setAttribute("ownPosition", ownPosition);
		session.setAttribute("ownValue", ownValue);
		session.setAttribute("favouritesBoard",favouritesBoard);
		session.setAttribute("favourites",favourites);
		Map<String, Double> topTen = LeaderboardController.getTopTen(userPages);
		session.setAttribute("allBoard",topTen);
		resp.sendRedirect("leaderboard.jsp?type=allPages");
	}

	public void getCoursePages(String courseName)throws IOException{
		Map<String, Double> userPages = Statistics.getTotalPagesByUserOfCourse(courseName);


		int ownPosition = LeaderboardController.getPos(Statistics.getTotalPagesByUserOfCourse(courseName),currentUser);
		double ownValue = Statistics.getCoursePagesPerUser(currentUser, courseName);

		LinkedHashMap<Integer, Double> favouritesBoard = new LinkedHashMap<Integer, Double>();
		List<String> favourites = currentUser.getFavourites();

		if (favourites!=null){
			for(String favourite: favourites){
				User user = UserRegistry.getInstance().getUser(favourite);
				favouritesBoard.put(LeaderboardController.getPos(Statistics.getTotalPagesByUserOfCourse(courseName), user), Statistics.getCoursePagesPerUser(user, courseName));


			}
		}
		session.setAttribute("size",Statistics.getTotalPagesByUserOfCourse(courseName).size());
		session.setAttribute("ownPosition", ownPosition);
		session.setAttribute("ownValue", ownValue);
		session.setAttribute("favouritesBoard",favouritesBoard);
		session.setAttribute("favourites",favourites);
		Map<String, Double> topTen = LeaderboardController.getTopTen(userPages);
		session.setAttribute("allBoard",topTen);

		resp.sendRedirect("leaderboard.jsp?type=allPages&course="+courseName);

	}

	public void getRelativeAllPages()throws IOException{
		Map<String, Double> userPages = Statistics.getTotalRatioByUser();

		int ownPosition = LeaderboardController.getPos(Statistics.getTotalRatioByUser(),currentUser);
		double ownValue = Statistics.getRatioPagesPerDurationAllCourses(currentUser);

		LinkedHashMap<Integer, Double> favouritesBoard = new LinkedHashMap<Integer, Double>();
		List<String> favourites = currentUser.getFavourites();

		if (favourites!=null){
			for(String favourite: favourites){
				User user = UserRegistry.getInstance().getUser(favourite);
				favouritesBoard.put(LeaderboardController.getPos(Statistics.getTotalRatioByUser(), user), Statistics.getRatioPagesPerDurationAllCourses(user));

			}
		}
		session.setAttribute("size",Statistics.getTotalRatioByUser().size());
		session.setAttribute("ownPosition", ownPosition);
		session.setAttribute("ownValue", ownValue);
		session.setAttribute("favouritesBoard",favouritesBoard);
		session.setAttribute("favourites",favourites);
		Map<String, Double> topTen = LeaderboardController.getTopTen(userPages);
		session.setAttribute("allBoard",topTen);
		resp.sendRedirect("leaderboard.jsp?type=relativeAllPages");
	}

	public void getRelativeCoursePages(String courseName) throws IOException{
		Map<String, Double>userPages = new LinkedHashMap<String, Double>();
		
		userPages = Statistics.getTotalRatioByUserOfCourse(courseName);
	
		int ownPosition = LeaderboardController.getPos(Statistics.getTotalRatioByUserOfCourse(courseName),currentUser);
		double ownValue = Statistics.getRatioPagesPerDurationCourse(currentUser, courseName);

		LinkedHashMap<Integer, Double> favouritesBoard = new LinkedHashMap<Integer, Double>();
		List<String> favourites = currentUser.getFavourites();

		if (favourites!=null){
			for(String favourite: favourites){
				User user = UserRegistry.getInstance().getUser(favourite);
				favouritesBoard.put(LeaderboardController.getPos(Statistics.getTotalRatioByUserOfCourse(courseName), user), Statistics.getRatioPagesPerDurationCourse(user, courseName));


			}
		}
		session.setAttribute("size",Statistics.getTotalRatioByUserOfCourse(courseName).size());
		session.setAttribute("ownPosition", ownPosition);
		session.setAttribute("ownValue", ownValue);
		session.setAttribute("favouritesBoard",favouritesBoard);
		session.setAttribute("favourites",favourites);
		Map<String, Double> topTen = LeaderboardController.getTopTen(userPages);
		session.setAttribute("allBoard",topTen);
		resp.sendRedirect("leaderboard.jsp?type=relativeAllPages&course="+courseName);
	}

}